﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;
using System.Collections.ObjectModel;
using System.Windows.Media;
using System.ComponentModel;
using System.Windows;
using System.Threading;


namespace WGM_Client_Net35
{
    public partial class ConnectionHandlerObject
    {
        public string _lastServerMessage = "";
        public string LastServerMessage 
        {
            get { return _lastServerMessage; }
            
        }
        public WGM_Service.UserOptions MyUserOptions { get; set; }
        public string CurrentUserName { get; set; }
        public string CurrentPassword { get; set; }
        public WGM_Service.ArrayOfString OnlineUsers { get; set; }
        private delegate void GenericDelegate();
        private delegate void ParameterizedGenericDelegate(object argument);
        private delegate void ParameterizedBoolDelegate(bool argument);
        private delegate void ParameterizedMessageDelegate(WGM_Service.WGMMessage argument);
        private delegate void ParameterizedOptionsDelegate(WGM_Service.UserOptions argument);
        private delegate void ReceiveMessageDelegate(string userName, LocalMessageItem message, bool isGlobal);
        private WGM_Service.ServiceSoapClient _wgmSvc = new WGM_Client_Net35.WGM_Service.ServiceSoapClient();
        private MainWindow _parent;
        public string MacAddress { get; set; }
        public ObservableCollection<ContactListItem> ContactListItems { get; set; }
        public DateTime LogonTime;
        public TimeSpan ServerTimeOffset { get; set; }
        private Queue<WGM_Service.WGMMessage> _outgoingQ = new Queue<WGM_Client_Net35.WGM_Service.WGMMessage>();
        public int ConnectionStatus { get; set; }
        public bool IsConnected { get; set; }
        public bool ConnectionInProgress { get; set; }
        public bool ConnectionError { get; set; }
        public bool DisconnectInProgress { get; set; }
        public DateTime LastConnectionBegin { get; set; }
        public DateTime LastConnectionEnd { get; set; }
        public ConnectionHandlerObject()
        {    
            ContactListItems = new ObservableCollection<ContactListItem>();
            ServerTimeOffset = new TimeSpan(0);
            DisconnectInProgress = false;
            foreach (NetworkInterface nif in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (nif.OperationalStatus == OperationalStatus.Up)
                {
                    MacAddress = nif.GetPhysicalAddress().ToString();
                    break;
                }
            }
        }
        public void SetMyUserOptions(WGM_Service.UserOptions myOptions)
        {
            MyUserOptions = myOptions;
            _parent.SetCurrentDisplayInfo(myOptions.DisplayName, myOptions.UserStatus);
        }
        public void SetClientWindow(MainWindow ui)
        {
            _parent = ui;
        }
        public void SendMessage(WGM_Service.WGMMessage message)
        {
            _outgoingQ.Enqueue(message);
        }

        public void RetrieveContacts(System.Windows.Threading.Dispatcher dispatcher, bool reSynch)
        {
            try
            {
                if (!reSynch)
                {
                    GenericDelegate clearList = new GenericDelegate(ClearListItems);
                    dispatcher.Invoke(clearList);
                    WGM_Service.UserOptions[] allUsers = new WGM_Client_Net35.WGM_Service.UserOptions[0];
                    bool error = false;
                    int currTry = 1;
                    int maxTries = 5;
                    do
                    {
                        try
                        {
                            
                            allUsers = _wgmSvc.GetAllUserOptions(CurrentUserName,CurrentPassword,out _lastServerMessage);                            
                            error = false;
                            
                        }
                        catch
                        {
                            error = true;
                            currTry++;
                            Thread.Sleep(500);
                        }
                    }while (error && currTry < maxTries);
                    if (error)
                    {
                        MessageBox.Show("There was an error contacting the server\r\nto retreive the contact list.\r\nThis is a fatal error and WGM cannot continue.", "WGM Fatal Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        IsConnected = false;
                        Application.Current.Shutdown();
                    }
                    foreach (WGM_Service.UserOptions uo in allUsers)
                    {
                        if (uo.UserName == null || uo.UserName.Trim() == "")
                            continue;
                        if (uo.UserName.ToLower().Trim() == CurrentUserName.ToLower().Trim())
                        {
                            MyUserOptions = uo;
                            
                            continue;
                        }
                        ContactListItem newItem = new ContactListItem(uo.UserName, uo.DisplayName, uo.UserStatus, uo.DisplayName + "-" + uo.UserStatus, uo);
                        newItem.SetContactData(uo);
                        ParameterizedGenericDelegate addDel = new ParameterizedGenericDelegate(AddCLItem);
                        dispatcher.Invoke(addDel, newItem);
                    }
                }
                ParameterizedGenericDelegate activate = new ParameterizedGenericDelegate(ActivateOnlineClients);
                OnlineUsers = _wgmSvc.GetOnlineUserNames(CurrentUserName,CurrentPassword,out _lastServerMessage);
                dispatcher.Invoke(activate, OnlineUsers);
            }
            catch { }

        }
        private void ClearListItems()
        {
            try
            {
                ContactListItems.Clear();
            }
            catch { }
        }
        private void AddCLItem(object item)
        {
            try
            {
                ContactListItem newCL = item as ContactListItem;
                if (newCL.DisplayName.Trim() == "")
                    newCL.DisplayName = newCL.UserName;
                for (int count = 0; count < ContactListItems.Count; count++)
                {
                    ContactListItem curr = ContactListItems[count];
                    if (string.Compare(newCL.DisplayName.Trim(), curr.DisplayName.Trim(), true) < 0)
                    {
                        ContactListItems.Insert(count, newCL);
                        return;
                    }
                }
                ContactListItems.Add(item as ContactListItem);
            }
            catch { }
        }
        private void ActivateOnlineClients(object onlineClients)
        {
            try
            {
                foreach (ContactListItem ci in ContactListItems)
                {
                    ci.SetContactOnline(IsUserOnline(ci.UserName, onlineClients as WGM_Service.ArrayOfString));
                }
            }
            catch { }
        }
        public bool IsUserOnline(string userName, WGM_Service.ArrayOfString onlineUsers)
        {
            try
            {
                foreach (string ou in onlineUsers)
                {
                    if (userName.ToLower().Trim() == ou.ToLower().Trim())
                    {
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }
        public void LogOn(string userName, string password)
        {
            try
            {
                string serverMessage = "";
                CurrentUserName = userName;
                CurrentPassword = password;
                IsConnected = _wgmSvc.UserLogin(userName, password, MacAddress, out serverMessage);

                LogonTime = _wgmSvc.GetServerTime();
                ServerTimeOffset = DateTime.Now - LogonTime;
                _lastServerMessage = serverMessage;
            }
            catch 
            {
                _lastServerMessage = "Error logging on, the server may be offline...";
            }
        }
        private void SendMessagesThreadProc(object argument)
        {            
            System.Windows.Threading.Dispatcher dispatcher = argument as System.Windows.Threading.Dispatcher;
            while (IsConnected)
            {
                if (_outgoingQ.Count > 0)
                {
                    WGM_Service.WGMMessage message = _outgoingQ.Dequeue();
                    try
                    {
                        
                        if (message.MessageType == WGM_Client_Net35.WGM_Service.MessageTypes.USER_LEAVING)
                        {
                            _wgmSvc.LogOff(message.SenderLogon, CurrentUserName, CurrentPassword,message.Message as string, out _lastServerMessage);
                        }
                        else
                        {
                            _wgmSvc.SendMessage(message, CurrentUserName, CurrentPassword);
                        }
                    } 
                    catch(Exception ex)
                    {
                        if (message.MessageType == WGM_Client_Net35.WGM_Service.MessageTypes.USER_LEAVING)
                        {
                            message.Message = "Log off user: " + message.SenderLogon;
                            message.MessageType = WGM_Client_Net35.WGM_Service.MessageTypes.GLOBAL_MESSAGE;
                        }
                        ParameterizedGenericDelegate del = new ParameterizedGenericDelegate(_parent.SendFailure);
                        dispatcher.Invoke(del, message);
                    }
                }
                Thread.Sleep(1000);
            }
        }
        private void RetreiveMessageThreadProc(object argument)
        {
            ConnectionStatus = 255;
            System.Windows.Threading.Dispatcher dispatcher = argument as System.Windows.Threading.Dispatcher;
            while (IsConnected)
            {
                try
                {
                    WGM_Service.WGMMessage []messages;
                    string returnMessage = "";
                    DateTime start = DateTime.Now;
                    LastConnectionBegin = start;
                    ConnectionInProgress = true;
                    
                    bool status = _wgmSvc.RetrieveMessages(CurrentUserName,CurrentPassword, MacAddress,out messages, out _lastServerMessage);
                    ConnectionError = false;
                    ConnectionInProgress = false;
                    if (status == true)
                    {
                        foreach (WGM_Service.WGMMessage message in messages)
                        {
                            switch (message.MessageType)
                            {
                                case WGM_Client_Net35.WGM_Service.MessageTypes.GLOBAL_MESSAGE:
                                    ReceiveGlobalMessage(message, dispatcher);
                                    break;
                                case WGM_Client_Net35.WGM_Service.MessageTypes.PRIVATE_MESSAGE:
                                    ReceivePrivateMessage(message, dispatcher);
                                    break;
                                case WGM_Client_Net35.WGM_Service.MessageTypes.USER_LEAVING:
                                    if (HelperFunctions.IsMessageValid(message,LogonTime,ServerTimeOffset))
                                    {
                                        ParameterizedMessageDelegate mdel = new ParameterizedMessageDelegate(ReceiveLeavingMessage);
                                        dispatcher.Invoke(mdel, message);                                        
                                    }
                                    break;
                                case WGM_Client_Net35.WGM_Service.MessageTypes.USER_LOGON:   
                                    if (HelperFunctions.IsMessageValid(message, LogonTime, ServerTimeOffset))
                                    {
                                        ParameterizedMessageDelegate mdel = new ParameterizedMessageDelegate(ReceiveLogonMessage);
                                        dispatcher.Invoke(mdel, message);
                                   }
                                    break;
                                case WGM_Client_Net35.WGM_Service.MessageTypes.USER_CHANGED_INFO:
                                    {
                                        if (HelperFunctions.IsMessageValid(message, LogonTime, ServerTimeOffset))
                                        {
                                            ParameterizedMessageDelegate mdel = new ParameterizedMessageDelegate(ReceiveInfoChangeMessage);
                                            dispatcher.Invoke(mdel, message);
                                        }
                                    }
                                    break;
                                case WGM_Client_Net35.WGM_Service.MessageTypes.NEW_USER_CREATED:
                                    {
                                        ParameterizedMessageDelegate mdel = new ParameterizedMessageDelegate(ReceiveNewUserMessage);
                                        dispatcher.Invoke(mdel, message);
                                    }
                                    break;
                                case WGM_Client_Net35.WGM_Service.MessageTypes.USER_CHANGED_IMAGE:
                                    {
                                        ParameterizedMessageDelegate mdel = new ParameterizedMessageDelegate(ReceiveUserImageMessage);
                                        dispatcher.Invoke(mdel, message);
                                    }
                                    break;
                            }
                        }
                    }
                    if (status == false && !DisconnectInProgress)
                    {
                        bool success = _wgmSvc.UserLogin(CurrentUserName, CurrentPassword, MacAddress, out returnMessage);
                        if (success)
                        {
                            _lastServerMessage = returnMessage;
                            LogonTime = _wgmSvc.GetServerTime();
                            RetrieveContacts(dispatcher, false);
                            ConnectionError = true;
                        }
                        else
                        {
                            IsConnected = false;
                            MessageBox.Show(DateTime.Now+" Unable to reconnect following a connection error", "WGM Fatal Error", MessageBoxButton.OK, MessageBoxImage.Error);
                            Application.Current.Shutdown();
                        }
                    }


                }
                catch(Exception ex)
                {
                    
                    ConnectionError = true;
                }

                if (ConnectionError)
                {
                    int conStat = 0;
                    ParameterizedGenericDelegate del = new ParameterizedGenericDelegate(_parent.SetConnectionStatus);
                    dispatcher.Invoke(del, conStat);
                }
                else
                {
                    int conStat = 254;
                    ParameterizedGenericDelegate del = new ParameterizedGenericDelegate(_parent.SetConnectionStatus);
                    dispatcher.Invoke(del, conStat);
                }
                Thread.Sleep(1000);
            }
        }

        private void ReceiveUserImageMessage(WGM_Client_Net35.WGM_Service.WGMMessage message)
        {
            try
            {
                ContactListItem changingContact = GetContactListItem(message.SenderLogon);
                if (changingContact != null)
                {
                    byte [] imageBytes = RetrieveUserImage(changingContact.UserName);
                    if (imageBytes != null)
                        changingContact.DisplayImage = HelperFunctions.ByteArrayToImage(imageBytes);
                    else
                        changingContact.DisplayImage = null;

                    _parent.SetRemoteDisplayImage(changingContact.UserName);

                }
            }
            catch { }
        }
        private void ReceiveNewUserMessage(WGM_Client_Net35.WGM_Service.WGMMessage message)
        {
            try
            {
                OnlineUsers.Add(message.SenderLogon);
                WGM_Service.UserOptions uo = _wgmSvc.GetUserOptions(message.SenderLogon,CurrentUserName,CurrentPassword,out _lastServerMessage);
                ContactListItem newItem = new ContactListItem(uo.UserName, uo.DisplayName, uo.UserStatus, uo.DisplayName + "-" + uo.UserStatus, uo);
                ContactListItems.Add(newItem);
                newItem.SetContactData(uo);                
                if (IsUserOnline(message.SenderLogon, OnlineUsers))
                    newItem.SetContactOnline(true);
            }
            catch
            {
            }
        }
        private void ReceiveInfoChangeMessage(WGM_Client_Net35.WGM_Service.WGMMessage message)
        {
            try
            {
                ContactListItem changingContact = GetContactListItem(message.SenderLogon);
                if (changingContact != null)
                {
                    WGM_Service.UserOptions newOptions = _wgmSvc.GetUserOptions(message.SenderLogon,CurrentUserName,CurrentPassword,out _lastServerMessage);
                    changingContact.ContactData = newOptions;
                    changingContact.SetContactData(newOptions);
                    ContactListItems.Remove(changingContact);
                    AddCLItem(changingContact);
                }
                TabListItem changingTab = _parent.FindOpenTab(message.SenderLogon, false, false);
                if (changingTab != null)
                    changingTab.SetTabData(GetUserOptions(message.SenderLogon), changingContact.IsContactOnline);
            }
            catch { }

        }

        private void ReceiveLogonMessage(WGM_Client_Net35.WGM_Service.WGMMessage message)
        {
            try
            {
                ContactListItem joiningContact = GetContactListItem(message.SenderLogon);
                if (joiningContact != null)
                    joiningContact.SetContactOnline(true);
                TabListItem joiningTab = _parent.FindOpenTab(message.SenderLogon, false, false);
                if (joiningTab != null)
                {
                    joiningTab.SetTabData(GetUserOptions(message.SenderLogon), true);
                }
                try
                {
                    bool found = false;
                    foreach (string user in OnlineUsers)
                    {
                        if (String.Compare(user, message.SenderLogon, true) == 0)
                        {
                            found = true;
                        }
                    }
                    if (!found)
                    {

                        OnlineUsers.Add(message.SenderLogon);
                        if (_parent.Visibility == Visibility.Hidden && Properties.Settings.Default.PopupContactOnline)
                        {
                            if (joiningContact != null)
                            {
                                _parent.TrayIcon.ShowBalloonTip(1500, "User joining at " + DateTime.Now + ":", HelperFunctions.GetIntroString(joiningContact.ContactData as WGM_Service.UserOptions), System.Windows.Forms.ToolTipIcon.Info);
                            }
                        }
                    }
                }
                catch
                { }
            }
            catch { }
 
        }

        private void ReceiveLeavingMessage(WGM_Client_Net35.WGM_Service.WGMMessage message)
        {
            try
            {
                if (message.SenderLogon.ToLower().Trim() == CurrentUserName.ToLower().Trim())
                {
                    IsConnected = false;
                    MessageBox.Show("At " + message.TimeStamp + ", " + message.ReceiverLogon + " has requested your WGM application to be shut down. Reason:\r\n" + message.Message as string, "WGM Beta", MessageBoxButton.OK, MessageBoxImage.Information);
                    Properties.Settings.Default.Save();
                    Application.Current.Shutdown();
                }
                ContactListItem leavingContact = GetContactListItem(message.SenderLogon);
                if (leavingContact != null)
                    leavingContact.SetContactOnline(false);
                TabListItem joiningTab = _parent.FindOpenTab(message.SenderLogon, false, false);
                if (joiningTab != null)
                {
                    joiningTab.SetTabData(GetUserOptions(message.SenderLogon), false);
                }
                try
                {
                    OnlineUsers.Remove(message.SenderLogon);
                    if (_parent.Visibility == Visibility.Hidden && Properties.Settings.Default.PopupContactLeaving)
                    {
                        if (leavingContact != null)
                        {
                            _parent.TrayIcon.ShowBalloonTip(1500, "User leaving at " + DateTime.Now + ":", HelperFunctions.GetIntroString(leavingContact.ContactData as WGM_Service.UserOptions), System.Windows.Forms.ToolTipIcon.Info);
                        }
                    }

                }
                catch { }
            }
            catch { }

        }

        private void ReceiveGlobalMessage(WGM_Client_Net35.WGM_Service.WGMMessage message, System.Windows.Threading.Dispatcher dispatcher)
        {
            ContactListItem cli = GetContactListItem(message.SenderLogon);
            WGM_Service.UserOptions uo;
            if (cli == null)
                uo = MyUserOptions;
            else
                uo = cli.ContactData as WGM_Service.UserOptions;

            LocalMessageItem lmi = new LocalMessageItem(message.TimeStamp, message.Message as string, HelperFunctions.GetNewColour(uo), message.SenderLogon, true, Colors.DarkGray.ToString());
            lmi.SenderFontFamily = new FontFamily(uo.UserFontFace);
            lmi.SenderFontSize = uo.FontSize;
            lmi.SenderFontStyle = HelperFunctions.GetFontStyle(uo.FontStyleInt);
            lmi.SenderFontWeight = HelperFunctions.GetFontWeight(uo.FontStyleInt);
            ReceiveMessageDelegate recDel = new ReceiveMessageDelegate(_parent.ReceiveMessage);
            dispatcher.Invoke(recDel, message.SenderLogon, lmi, true);

        }
        private void ReceivePrivateMessage(WGM_Service.WGMMessage message, System.Windows.Threading.Dispatcher dispatcher)
        {
            ContactListItem cli  = GetContactListItem(message.SenderLogon);
            WGM_Service.UserOptions uo;
            if (cli == null)
                uo = MyUserOptions;
            else
                uo = cli.ContactData as WGM_Service.UserOptions;

            LocalMessageItem lmi = null;
            string receiverTab = message.SenderLogon;
            if (message.SenderLogon.ToLower().Trim() == CurrentUserName.ToLower().Trim())
                receiverTab = message.ReceiverLogon;
            lmi = new LocalMessageItem(message.TimeStamp, message.Message as string, HelperFunctions.GetNewColour(uo), message.SenderLogon, true, Colors.DarkGray.ToString());
            lmi.SenderFontFamily = new FontFamily(uo.UserFontFace);
            lmi.SenderFontSize = uo.FontSize;
            lmi.SenderFontStyle = HelperFunctions.GetFontStyle(uo.FontStyleInt);
            lmi.SenderFontWeight = HelperFunctions.GetFontWeight(uo.FontStyleInt);
            ReceiveMessageDelegate recDel = new ReceiveMessageDelegate(_parent.ReceiveMessage);            
            dispatcher.Invoke(recDel, receiverTab, lmi, false);
           
        }

        private string GetIntroString(string userName)
        {
            try
            {
                foreach (ContactListItem cli in ContactListItems)
                {
                    if (cli.UserName.ToLower().Trim() == userName.ToLower().Trim())
                    {
                        return cli.DisplayName;
                    }
                }
                return null;
            }
            catch
            {
                return null;
            }
        }
        public void Execute(System.Windows.Threading.Dispatcher dispatcher)
        {
            
            BackgroundWorker bg = new BackgroundWorker();
            bg.DoWork += new DoWorkEventHandler(bw_DoWork);
            bg.RunWorkerCompleted +=new RunWorkerCompletedEventHandler( bw_RunWorkerCompleted);
            bg.RunWorkerAsync(dispatcher);
        }
        public ContactListItem GetContactListItem(string userName)
        {
            try
            {
                foreach (ContactListItem cli in ContactListItems)
                {
                    if (cli.UserName.ToLower().Trim() == userName.ToLower().Trim())
                        return cli;
                }
                return null;
            }
            catch
            {
                return null;
            }
        }
        public WGM_Service.UserOptions GetUserOptions(string userName)
        {
            try
            {
                if (userName == null)
                    return null;
                if (userName.Trim().ToLower() == CurrentUserName.ToLower().Trim())
                    return MyUserOptions;
                foreach (ContactListItem cli in ContactListItems)
                {
                    if (cli.UserName.ToLower().Trim() == userName.ToLower().Trim())
                        return cli.ContactData as WGM_Service.UserOptions;
                }
                return null;
            }
            catch
            {
                return null;
            }
        }
        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {            
                RetrieveContacts(e.Argument as System.Windows.Threading.Dispatcher,false);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void ReSyncThreadProc(object argument)
        {
            System.Windows.Threading.Dispatcher dispatcher = argument as System.Windows.Threading.Dispatcher;
            do
            {
                Thread.Sleep(1000 * 60 * 5);
                RetrieveContacts(dispatcher,true);
            }
            while (IsConnected);
        }
        void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

            Thread retrieve = new Thread(new ParameterizedThreadStart(RetreiveMessageThreadProc));
            Thread send = new Thread(new ParameterizedThreadStart(SendMessagesThreadProc));
            Thread resynch = new Thread(new ParameterizedThreadStart(ReSyncThreadProc));
            Thread getUserImagesThread = new Thread(new ParameterizedThreadStart(GetUserImagesThreadProc));
            getUserImagesThread.IsBackground = true;
            resynch.IsBackground = true;
            send.IsBackground = true;
            retrieve.IsBackground = true;

            _parent.SetCurrentDisplayInfo(MyUserOptions.DisplayName, MyUserOptions.UserStatus);
            IsConnected = true;
            retrieve.Start(System.Windows.Threading.Dispatcher.CurrentDispatcher);
            send.Start(System.Windows.Threading.Dispatcher.CurrentDispatcher);
            resynch.Start(System.Windows.Threading.Dispatcher.CurrentDispatcher);
            getUserImagesThread.Start(System.Windows.Threading.Dispatcher.CurrentDispatcher);
            _parent.AddMainWindow();
        }
        private void GetUserImagesThreadProc(object argument)
        {
            System.Windows.Threading.Dispatcher dispatcher = argument as System.Windows.Threading.Dispatcher;
            while (IsConnected)
            {
                try
                {
                    foreach (ContactListItem cli in ContactListItems)
                    {
                        byte[] userImage = RetrieveUserImage(cli.UserName);
                        if (userImage != null)
                        {
                            cli.DisplayImage = HelperFunctions.ByteArrayToImage(userImage);
                            ParameterizedGenericDelegate del = new ParameterizedGenericDelegate(_parent.SetRemoteDisplayImage);

                            dispatcher.Invoke(del,cli.UserName);
                        }
                    }
                }
                catch { }
                Thread.Sleep(1000 * 60 * 20);
            }
        }

        public byte[] RetrieveUserImage(string userName)
        {
            try
            {
                return _wgmSvc.GetUserImage(userName,CurrentUserName,CurrentPassword,out _lastServerMessage);
            }
            catch(Exception ex)
            {
                return null;
            }
        }
        public bool SetUserImage(string userName, byte[] image)
        {
            try
            {
                _wgmSvc.SetUserImage(CurrentUserName,CurrentPassword, image);
            }
            catch
            {
                return false;
            }
            return true;
        }
        public bool SetUserOptions(string userName, WGM_Service.UserOptions options)
        {
            try
            {
                _wgmSvc.SetUserOptions(CurrentUserName,CurrentPassword,options);
            }
            catch
            {
                return false;
            }
            return true;
        }
        public bool CreateAccount(string userName, string password, string secureQ, string secureA, out string message)
        {
            try
            {
               return _wgmSvc.CreateUser(userName, password, secureQ, secureA,out message);
            }
            catch (Exception ex)
            {
                message = "There was an error contacting the server";
                return false;
            }
        }
        public void LogOff()
        {
            DisconnectInProgress = true;
            IsConnected = false;
            try
            {
                _wgmSvc.LogOff(CurrentUserName,CurrentUserName,CurrentPassword,null, out _lastServerMessage);
            }
            catch { }
            
        }

    }
}
